package com.winchain.chainbill.bill.biz.report.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.biz.report.service.BizBoardService;
import com.winchain.chainbill.bill.biz.report.vo.BizBoardQueryVo;
import com.winchain.chainbill.bill.biz.report.vo.BizBoardStatsVo;
import com.winchain.chainbill.bill.biz.send.vo.BillSnd;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BizBoardController extends BaseController {
	@Autowired
	private BizBoardService bizBoardService;

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> getAllStats(String grpId) {
		Map<String, Object> map = new HashMap();
		map.put("cusStats", Integer.valueOf(this.bizBoardService.countCustomerByGrpId(grpId)));
		map.put("sendAmts", this.bizBoardService.countSendAmts(grpId));
		map.put("transferAmts", this.bizBoardService.countTransferAmts(grpId));
		map.put("dctAmts", this.bizBoardService.countDctAmts(grpId));
		return map;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������")
	public Map<String, Object> queryBarStats(String grpId, String ids, String startTime, String endTime, String type)
			throws Exception {
		Map<String, Object> map = new HashMap();
		String maxDate = getEndDate(endTime);
		String minDate = getStartDate(startTime);
		List<BizBoardStatsVo> list = null;
		Map<String, Object> repurMap = null;
		if ("1".equals(type)) {
			list = this.bizBoardService.selectCustomersForBoard(grpId, minDate, maxDate, ids);
		} else if ("2".equals(type)) {
			list = this.bizBoardService.querySendBillsForBoard(grpId, minDate, maxDate, ids);
		} else if ("3".equals(type)) {
			list = this.bizBoardService.queryXfeBillsForBoard(grpId, minDate, maxDate, ids);
		} else if ("4".equals(type)) {
			list = this.bizBoardService.queryDctBillsForBoard(grpId, minDate, maxDate, ids);
			repurMap = createRepurMap(grpId, minDate, maxDate);
		}
		if (list != null) {
			List<String> queryMonths = new ArrayList();
			map = restructureMap(queryMonths, list);
			List<String> months = SysDateUtil.getMonthBetween(minDate, maxDate);
			int count = 0;
			for (String month : months) {
				if (!queryMonths.contains(month)) {
					BizBoardStatsVo busBoardVo = new BizBoardStatsVo();
					busBoardVo.setNum("0");
					busBoardVo.setDate(month);
					busBoardVo.setTxAmt(BigDecimal.ZERO);
					list.add(count, busBoardVo);
				}
				if (type.equals("4")) {
					BizBoardStatsVo boardVo = (BizBoardStatsVo) list.get(count);
					restructureStatsVo(boardVo, month, repurMap);
				}
				count++;
			}
			map.put("list", list);
		}
		return map;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public Map<String, Object> queryDistributionStats(String grpId, String type) {
		if (type.equals("1")) {
			List<BizBoardQueryVo> queryVos = this.bizBoardService.selectCustomersForDisBoard(grpId);
			return restructureQueryVo(queryVos, true);
		}
		if (type.equals("2")) {
			List<BizBoardQueryVo> boardQueryVos = this.bizBoardService.selectBillSndsByGroupId(grpId);
			Integer expNum = Integer.valueOf(0);
			BigDecimal expAmt = BigDecimal.ZERO;
			Integer unExpNum = Integer.valueOf(0);
			BigDecimal unExpAmt = BigDecimal.ZERO;
			List<BillSnd> billSnds = this.bizBoardService.selectNotGrouByBillSndsByGroupId(grpId);
			for (BillSnd billSnd : billSnds) {
				if (SysDateUtil.compare(billSnd.getExpDate(), SysDateUtil.getSystemDateTime("yyyyMMdd")) <= 0) {
					expNum = Integer.valueOf(expNum.intValue() + 1);
					expAmt = expAmt.add(billSnd.getBillAmt());
				} else {
					unExpNum = Integer.valueOf(unExpNum.intValue() + 1);
					unExpAmt = unExpAmt.add(billSnd.getBillAmt());
				}
			}
			Object map = new HashMap();
			((Map) map).put("expNum", expNum);
			((Map) map).put("unExpNum", unExpNum);
			((Map) map).put("expAmt", expAmt);
			((Map) map).put("unExpAmt", unExpAmt);
			((Map) map).put("list", boardQueryVos);
			((Map) map).put("totalAmt", expAmt.add(unExpAmt));
			((Map) map).put("totalNum", Integer.valueOf(0));
			return (Map<String, Object>) map;
		}
		if (type.equals("3")) {
			List<BizBoardQueryVo> queryVos = this.bizBoardService.selectBillXfesByGroupId(grpId);
			Map<String, Object> result = restructureQueryVo(queryVos, false);
			result.put("splitXfeNum", this.bizBoardService.countSplitXfeBillByGroupId(grpId));
			result.put("fullXfeNum", this.bizBoardService.countFullXfeBillByGroupId(grpId));
			return result;
		}
		if (type.equals("4")) {
			List<BizBoardQueryVo> queryVos = this.bizBoardService.selectDctsForDisBoard(grpId);
			return restructureQueryVo(queryVos, false);
		}
		return new HashMap();
	}

	private String createStartDateByCurrentYear() {
		Calendar localTime = Calendar.getInstance();
		return localTime.get(1) + "0101";
	}

	private String getStartDate(String startDate) {
		if (StringUtil.empty(startDate)) {
			return createStartDateByCurrentYear();
		}
		return startDate + "01";
	}

	private String getEndDate(String endDate) {
		if (StringUtil.empty(endDate)) {
			return SysDateUtil.getSystemDateTime("yyyyMMdd");
		}
		if (endDate.length() <= 6) {
			endDate = endDate + "01";
		}
		return SysDateUtil.getDateOffsetWithDayType(endDate, 0, 1);
	}

	private Map<String, Object> createRepurMap(String grpId, String minDate, String maxDate) {
		List<BizBoardStatsVo> repurList = this.bizBoardService.queryRpurBillsForBoard(grpId, minDate, maxDate);
		if (repurList != null) {
			Map<String, Object> repurMap = new HashMap();
			for (BizBoardStatsVo repurVo : repurList) {
				repurMap.put(repurVo.getDate(), repurVo);
			}
			return repurMap;
		}
		return null;
	}

	private Map<String, Object> restructureMap(List<String> queryMonths, List<BizBoardStatsVo> list) {
		Map<String, Object> map = new HashMap();
		Integer totalNum = Integer.valueOf(0);
		BigDecimal totalAmt = BigDecimal.ZERO;
		for (BizBoardStatsVo busBoardVo : list) {
			queryMonths.add(busBoardVo.getDate());
			totalNum = Integer.valueOf(totalNum.intValue() + Integer.valueOf(busBoardVo.getNum()).intValue());
			if (busBoardVo.getTxAmt() != null) {
				totalAmt = totalAmt.add(busBoardVo.getTxAmt());
			}
		}
		map.put("totalNum", totalNum);
		map.put("totalAmt", totalAmt);
		return map;
	}

	private void restructureStatsVo(BizBoardStatsVo statsVo, String month, Map<String, Object> repurMap) {
		statsVo.setRepurDate(month);
		statsVo.setRepurAmt(BigDecimal.ZERO);
		if ((repurMap != null) && (repurMap.size() > 0)) {
			BizBoardStatsVo repurVo = (BizBoardStatsVo) repurMap.get(month);
			if (repurVo != null) {
				statsVo.setRepurAmt(repurVo.getRepurAmt());
				statsVo.setRepurNum(repurVo.getNum());
			}
		}
		BigDecimal avgDaily = BigDecimal.ZERO;
		if (statsVo.getTxAmt().compareTo(BigDecimal.ZERO) > 0) {
			int days = SysDateUtil.getDaysByYearMonth(Integer.valueOf(month.substring(0, 4)).intValue(),
					Integer.valueOf(month.substring(5, month.length())).intValue());
			avgDaily = statsVo.getTxAmt().divide(new BigDecimal(days), 2, RoundingMode.HALF_DOWN);
		}
		statsVo.setDctDailyAmt(avgDaily);
	}

	private Map<String, Object> restructureQueryVo(List<BizBoardQueryVo> queryVos, boolean valueFalg) {
		List<BizBoardQueryVo> list = new ArrayList();
		Map<String, Object> cusMap = new LinkedHashMap();
		for (BizBoardQueryVo queryVo : queryVos) {
			String tagName = "";
			String tagNames = queryVo.getTagNames();
			String tagCodeStrs = queryVo.getTagCodes();
			if (tagCodeStrs.contains("FOCAL_CO")) {
				tagName = getTagName(tagNames, tagCodeStrs.split(","), "FOCAL_CO");
			} else if (tagCodeStrs.contains("MEMBER")) {
				tagName = getTagName(tagNames, tagCodeStrs.split(","), "MEMBER");
			} else {
				tagName = coverLvl(Integer.valueOf(queryVo.getCusLvl())) + "��������";
			}
			Object currentNum = cusMap.get(tagName);
			if (currentNum == null) {
				if (valueFalg) {
					cusMap.put(tagName, Integer.valueOf(1));
				} else {
					cusMap.put(tagName, queryVo.getQueryAmt());
				}
			} else if (valueFalg) {
				cusMap.put(tagName, Integer.valueOf(Integer.valueOf(currentNum.toString()).intValue() + 1));
			} else {
				cusMap.put(tagName, new BigDecimal(currentNum.toString()).add(queryVo.getQueryAmt()));
			}
		}
		Integer totalNum = Integer.valueOf(0);
		BigDecimal totalAmt = BigDecimal.ZERO;
		for (String tagName2 : cusMap.keySet()) {
			BizBoardQueryVo distriVo = new BizBoardQueryVo();
			distriVo.setCusName(tagName2);
			if (valueFalg) {
				distriVo.setRegisterNum(Integer.valueOf(cusMap.get(tagName2).toString()));
				totalNum = Integer.valueOf(totalNum.intValue() + distriVo.getRegisterNum().intValue());
			} else {
				distriVo.setQueryAmt(new BigDecimal(cusMap.get(tagName2).toString()));
				totalAmt = totalAmt.add(distriVo.getQueryAmt());
			}
			list.add(distriVo);
		}
		Map<String, Object> result = new LinkedHashMap();
		result.put("totalNum", totalNum);
		result.put("totalAmt", totalAmt);
		result.put("list", list);
		return result;
	}

	private String coverLvl(Integer cusLvl) {
		String[] str = { "��", "��", "��", "��", "��", "��", "��", "��", "��", "��" };
		if ((cusLvl == null) || (cusLvl.intValue() == 0)) {
			return "��";
		}
		return str[(cusLvl.intValue() - 1)];
	}

	private String getTagName(String tagNames, String[] tagCodes, String containsCode) {
		int count = 0;
		for (String tagCode : tagCodes) {
			if (tagCode.equals(containsCode)) {
				break;
			}
			count++;
		}
		String[] names = tagNames.split(",");
		return names[count];
	}
}
